package DispatchSys;

import Shared.Form;
import Shared.Telephone;
import Simulator.ER;

/**
 * The Operator module, see Architecture report for specifications.
 */

public class Operator implements Runnable {

	// Form recorder module
	private FormRecorder recorder;
	
	/**
	 * Creates an Operator object
	 */
	public Operator() {
		recorder = new FormRecorder();
	}
	
	/**
	 * Function that will simulate the behavior of the
	 * operator (answering phone and filling forms)
	 */
	@Override
	public void run() {
		try {
			while (true) {
				// taking an emergency
				ER emergency = Telephone.answer();
				
				// Translating it into a form
				Form form = translateEmergency(emergency);
				
				// Storing the form via the FormRecoder module
				recorder.enqueueForm(form);
			}
		} catch (InterruptedException e) {
			Thread.currentThread().interrupt();
		}

	}
	
	/**
	 * Translates an emergency into a form
	 * @param emergency - the emergency to be translated
	 */
	private Form translateEmergency(ER emergency) {
		return new Form(emergency.getCalleeName(), emergency.getCalleeNumber(),
				emergency.getCalleePosition(), emergency.isUrgent(),
				emergency.getLocation(), emergency.getDescription());
	}

}
